package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.TabDataSettingMapper;
import com.powergisol.gis.dao.TabProjectMapper;
import com.powergisol.gis.dao.TabYcMapper;
import com.powergisol.gis.entity.TabDataSetting;
import com.powergisol.gis.entity.TabProject;
import com.powergisol.gis.entity.TabYc;
import com.powergisol.gis.service.TabProjectService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabDataSettingVO;
import com.powergisol.gis.vo.TabProjectVo;
import com.powergisol.gis.vo.TabServiceVo;
import javafx.scene.control.Tab;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TabProjectServiceImpl implements TabProjectService {
    @Autowired
    TabProjectMapper tabProjectMapper;

    @Autowired
    TabYcMapper tabYcMapper;

    @Autowired
    TabDataSettingMapper tabDataSettingMapper;

    @Override
    public TabProject getOne(Integer settingId) {
        return tabProjectMapper.getOne(settingId);
    }

    @Override
    public List<TabProject> getByUuid(String uuid) {
        TabYc yc = tabYcMapper.selectYcByUuid(uuid);
        if(null == yc.getSettingId()){
            return null;
        }
        List<TabProject> projects = tabProjectMapper.getBySettingId(yc.getSettingId());
        return projects;
    }

    /**
     * 获取所有的项目名称
     * @return
     */
    @Override
    public Result getProjectAllClass() {
        List<TabProjectVo> projectList = tabProjectMapper.getProjectAllClass();
        /*projectList.forEach(x->{
            TabDataSetting one = tabDataSettingMapper.getOne(x.getSettingId());
            if(one!=null){
                x.setServiceId(one.getServiceId());
            }
        });*/
        return new Result().success(projectList);
    }

    @Override
    public Result getProjectAllClassTwo() {
        List<TabDataSetting> dataSettings = tabDataSettingMapper.selectAll();
        List<TabDataSettingVO> tabDataSettingVOList = new ArrayList<>();
        List<Integer> ids = dataSettings.stream().map(d -> d.getId()).collect(Collectors.toList());
        List<Integer> serviceIds = dataSettings.stream().map(m -> m.getServiceId()).collect(Collectors.toList());
        List<TabProject> tabProjects =  tabProjectMapper.getByIdS(ids);

        List<TabServiceVo>  tabServices= tabDataSettingMapper.getByServiceUuid(serviceIds);
        for (TabDataSetting dataSetting : dataSettings) {
            TabDataSettingVO tabDataSettingVO = new TabDataSettingVO();
            BeanUtils.copyProperties(dataSetting,tabDataSettingVO);
            TabProject one = new TabProject();
            for (TabProject tabProject : tabProjects) {
                if(tabProject.getId().equals(dataSetting.getId())){
                    BeanUtils.copyProperties(tabProject,one);
                }
            }
            if(one!=null){
                tabDataSettingVO.setGCMC(one.getGCMC());
            }
            String name = null;
            for (TabServiceVo tabService : tabServices) {
                if(tabService.getId().equals(dataSetting.getServiceId())){
                    name = tabService.getName();
                }
            }
            if(name!=null){
                tabDataSettingVO.setServiceName(name);
            }
            tabDataSettingVOList.add(tabDataSettingVO);
        }
        return new Result().success(tabDataSettingVOList);
    }

    @Override
    public List<TabProject> getAllBySet(List<Integer> settingIds) {
        return tabProjectMapper.getAllBySet(settingIds);
    }
}
